package cn.ljy.uem.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.ProjectMapper;
import cn.ljy.uem.dao.ProjectTeamMapper;
import cn.ljy.uem.model.ProjectTeam;
import cn.ljy.uem.model.query.ProjectTeamQuery;
import cn.ljy.uem.service.ProjectTeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * @description 项目组模块的业务层接口
 * @author  廖振豪
 * @date 2023-1-18
 */
@Service
@Slf4j
public class IProjectTeamService extends IBaseService<ProjectTeam, ProjectTeamQuery> implements ProjectTeamService {

    @Autowired
    private ProjectTeamMapper mapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    protected void beforeGet(Serializable id) {
        if (mapper.findById(id) == null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void afterGet(ProjectTeam projectTeam) {
        if (projectTeam.getProjectId()!=null){
            projectTeam.setProject(projectMapper.findById(projectTeam.getProjectId()));
        }
    }

    @Override
    protected void beforeUpdate(ProjectTeam projectTeam) {

    }


    @Override
    protected void beforeSave(ProjectTeam projectTeam) {
        if (projectMapper.findById(projectTeam.getProjectId())==null){
            throw new MyWebException("添加失败：请选择项目");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        //后续需添加
    }

    @Override
    protected void beforeFindAll(ProjectTeamQuery query) {

    }

    //项目组多表查询
    @Override
    protected void afterFindAll(List<ProjectTeam> list) {
        for (ProjectTeam projectTeam : list){
            if (projectTeam.getProjectId()!=null){
                projectTeam.setProject(projectMapper.findById(projectTeam.getProjectId()));
            }
        }
    }

}
